require 'date'

class Quote
  include DataMapper::Resource

  storage_names[:default] = 'mini_sp'

  property :Id,         Serial
  property :Datetime,   DateTime
  property :Price,      Integer
  property :Price_Desc, String
  property :Volume,     Integer
  property :Comment,    String

  @@tz = DateTime.now.offset

  def initialize(qoute_string)
    if qoute_string.index(/\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{1,2}:\d{1,2}\s[AP]M\s+\d+[AB]*\s+\d+\s+[\w+-]*/)!= nil
      @s_quote = qoute_string
      s_date = qoute_string.scan(/\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{1,2}:\d{1,2}\s[AP]M\s+/)
      dt = DateTime.strptime(s_date[0], "%m/%d/%Y %I:%M:%S %p")
      self.Datetime = DateTime.civil(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, @@tz)
      qoute_string.gsub!(s_date[0], "")

      arr_price = qoute_string.scan(/\d+[AB]*\s+/)
      s_price = arr_price[0]
      qoute_string.gsub!(arr_price[0], "")

      arr_price2 = s_price.scan(/\d+/)
      self.Price = Integer(arr_price2[0])
      s_price.gsub!(arr_price2[0], "")
      self.Price_Desc = s_price

      s_volume = qoute_string.scan(/\d+\s+/)
      self.Volume = Integer(s_volume[0])
      qoute_string.gsub!(s_volume[0], "")

      arr_comment = qoute_string.scan(/[\w+-]*/)
      self.Comment = arr_comment[0]

    else
      raise 'Cannot parse string'
    end
  end

  def to_s
    return self.Datetime.to_s() + "\t" + self.Price.to_s() + self.Price_Desc.to_s() + "\t" + self.Volume.to_s() + "\t" + self.Comment.to_s()
  end
end
